package com.glory.dgcpcpda.activity.technicalservice;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class GetSvidParamActivity extends BaseActivity{


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etSvidCode)
    FreeUI_ClearEditText etSvidCode;
    @BindView(R.id.tvSvidValue)
    TextView tvSvidValue;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_getsvidparam;
    }

    @Override
    protected void initView() {
        tvTitle.setText("SVID参数获取");
        setMainLayout(llMain);

    }

    @Override
    protected void initData() {

        initEdit(etEqpID,s ->{
            etSvidCode.setFocusable(true);
            etSvidCode.requestFocus();
            etSvidCode.setFocusableInTouchMode(true);
            etSvidCode.requestFocusFromTouch();
        });

        initEdit(etSvidCode,s->sendRequest());
    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
        sendRequest();
    }

    private void sendRequest(){
        if (getText(etEqpID).equals("")){
            showToast("请扫描设备号",false);
            return;
        }
        if (getText(etSvidCode).equals("")){
            showToast("请输入SVID",false);
            return;
        }
        FreeUI_EntityCallBack<BaseResponseBean<JsonObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JsonObject>>
                (new TypeToken<BaseResponseBean<JsonObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JsonObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    tvSvidValue.setText(response.body().getResponse().getBody().get("Value").getAsString());
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<JsonObject>> response) {
                super.onError(response);
                loadError(response.getException(), "OPPORTUNITYLOGEVENT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EqpId", getText(etEqpID));
        map.addProperty("SvidCode", getText(etSvidCode));
        NetUtils.requestPostNet("","GetPrarmBySvid", this, map, callBack);
    }
}
